package cn.edu.sgu.www.mhxysy.service.school.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.school.SchoolSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.school.SchoolSkillCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.school.SchoolSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.school.SchoolSkillCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.school.SchoolSkillCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class SchoolSkillCategoryServiceImpl implements SchoolSkillCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final SchoolSkillCategoryMapper schoolSkillCategoryMapper;

	@Autowired
	public SchoolSkillCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			SchoolSkillCategoryMapper schoolSkillCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.schoolSkillCategoryMapper = schoolSkillCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_MPJN;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(SchoolSkillCategoryDTO schoolSkillCategoryDTO) {
		SchoolSkillCategory schoolSkillCategory = toEntity(schoolSkillCategoryDTO);

		schoolSkillCategory.setId(null);

		schoolSkillCategoryMapper.insert(schoolSkillCategory);
	}

	@Override
	public void updateById(SchoolSkillCategoryDTO schoolSkillCategoryDTO) {
		ObjectUtils.requireNonNull(schoolSkillCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		SchoolSkillCategory schoolSkillCategory = toEntity(schoolSkillCategoryDTO);

		schoolSkillCategoryMapper.updateById(schoolSkillCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<SchoolSkillCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				schoolSkillCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public Page<SchoolSkillCategory> selectByPage(SchoolSkillCategoryPager pager) {
		QueryWrapper<SchoolSkillCategory> queryWrapper = new QueryWrapper<>();
		Page<SchoolSkillCategory> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getSchoolId() != null,
				"school_id", pager.getSchoolId()
		);

		return schoolSkillCategoryMapper.selectPage(page, queryWrapper);
	}

	private SchoolSkillCategory toEntity(SchoolSkillCategoryDTO schoolSkillCategoryDTO) {
		Integer id = schoolSkillCategoryDTO.getId();
		String name = schoolSkillCategoryDTO.getName();

		QueryWrapper<SchoolSkillCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ne(id != null, "id", id);
		queryWrapper.eq("name", name);

		Long count = schoolSkillCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "技能名称重复。");
		}

		SchoolSkillCategory schoolSkillCategory = new SchoolSkillCategory();

		schoolSkillCategory.setId(schoolSkillCategoryDTO.getId());
		schoolSkillCategory.setName(schoolSkillCategoryDTO.getName());
		schoolSkillCategory.setNote(schoolSkillCategoryDTO.getNote());
		schoolSkillCategory.setEfficacy(schoolSkillCategoryDTO.getEfficacy());
		schoolSkillCategory.setSchoolId(schoolSkillCategoryDTO.getSchoolId());
		schoolSkillCategory.setPropertyId(schoolSkillCategoryDTO.getPropertyId());
		schoolSkillCategory.setImproveEffect(schoolSkillCategoryDTO.getImproveEffect());
		schoolSkillCategory.setPropertyValue(schoolSkillCategoryDTO.getPropertyValue());

		return schoolSkillCategory;
	}

}